{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": 3
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python_defaultSpec_1597148667390",
   "display_name": "Python 3.6.4 64-bit ('base': conda)"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "url = r\"https://baike.baidu.com/item/%E9%83%91%E5%B8%8C%E6%80%A1/805307\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.get(url)\n",
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# r.text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 设置请求头，用于浏览器识别，包含系统版本，浏览器，浏览器内核等\n",
    "headers = {\n",
    "    \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.get(url, headers=headers)\n",
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# r.text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "# re.S表示可以包括换行\n",
    "pattern = re.compile('<div class=\"summary-pic\">.*?</div>',re.S)\n",
    "titles = re.findall(pattern, r.text)\n",
    "titles"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 如果无法导入，则pip install beautifulsoup4\n",
    "from bs4 import BeautifulSoup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# lxml是一种HTML解析器，也可以用html.parser等等；如果报错，可以pip install lxml\n",
    "bs = BeautifulSoup(r.text, \"lxml\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 选择 class=\"summary-pic\" 的标签里的 a 标签\n",
    "pic_range = bs.select(\".summary-pic a\")\n",
    "pic_range"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "relative_url = pic_range[0].get(\"href\")\n",
    "relative_url"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 以上得到相对路径，需要在根目录的基础上增加\n",
    "pic_list_url = \"https://baike.baidu.com\" + relative_url\n",
    "pic_list_url"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pic_list_response = requests.get(pic_list_url, headers=headers)\n",
    "pic_list_response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bs = BeautifulSoup(pic_list_response.text, \"lxml\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 选择 class=\"pic-list\" 的标签里的 img 标签\n",
    "pic_list_html = bs.select(\".pic-list img\")\n",
    "pic_list_html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "pic_urls = []\n",
    "for pic_html in pic_list_html:\n",
    "    # 获取每个img标签中的src属性，即每张图片的地址\n",
    "    pic_url = pic_html.get(\"src\")\n",
    "    pic_urls.append(pic_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pic_urls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "name = \"郑希怡\"\n",
    "path = \"小姐姐\"\n",
    "if not os.path.exists(path):  # 若文件夹不存在则创建\n",
    "    os.mkdir(path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 下载图片并保存\n",
    "for i, pic_url in enumerate(pic_urls):\n",
    "    pic = requests.get(pic_url, timeout=15)\n",
    "\n",
    "    # 合成图片的文件名\n",
    "    pic_name = name + \"_\" + str(i + 1) + \".jpg\"\n",
    "    filename = os.path.join(path, pic_name)\n",
    "\n",
    "    with open(filename, \"wb\") as f:\n",
    "        f.write(pic.content)\n",
    "        print(\"成功下载：%s\" % (filename))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}